
case class Music(title: String, artist: String, duration: Int) {  
  override def toString: String = s"$title by $artist (${duration}s)"  
}  
  

object PlayMode extends Enumeration {  
  type PlayMode = Value  
  val Normal, Shuffle, Repeat = Value  
}  
 
class MusicPlayer {  
  import PlayMode._  
  

  private var musicList: List[Music] = List.empty  
  
 
  private var currentIndex: Int = -1  
  
  private var playMode: PlayMode = Normal  
   
  def addMusic(music: Music): Unit = {  
    musicList = music :: musicList  
  }  
  
  def deleteMusic(music: Music): Unit = {  
    musicList = musicList.filterNot(_ == music)   
    if (currentIndex >= 0 && musicList(currentIndex) == music) {  
      currentIndex = -1  
    }  
  }  
   
  def play(): Unit = {  
    if (musicList.isEmpty) {  
      println("No music in the playlist.")  
    } else {  
      currentIndex = getIndexToPlay()  
      val currentMusic = musicList(currentIndex)  
      println(s"Playing: ${currentMusic}")  
    }  
  }  
  
  private def getIndexToPlay(): Int = {  
    playMode match {  
      case Normal =>  
        if (currentIndex < 0 || currentIndex >= musicList.size) {  
          0 
        } else {  
          currentIndex + 1 
        }  
      case Shuffle =>  
        scala.util.Random.nextInt(musicList.size) 
      case Repeat =>  
        currentIndex 
    }  
  }  
  
 
  def setPlayMode(mode: PlayMode): Unit = {  
    playMode = mode  
  }  
  
 
  def getPlayMode: PlayMode = {  
    playMode  
  }  
  
 
  def getCurrentMusic: Option[Music] = {  
    if (currentIndex >= 0 && currentIndex < musicList.size) {  
      Some(musicList(currentIndex))  
    } else {  
      None  
    }  
  }  
  
  
  def getMusicList: List[Music] = {  
    musicList  
  }  
  
 
  def reset(): Unit = {  
    currentIndex = -1  
  }  
}  
  

object MusicPlayerDemo {  
  def main(args: Array[String]): Unit = {  
    val player = new MusicPlayer  
  
  
    player.addMusic(Music("Song 1", "Artist 1", 120))  
    player.addMusic(Music("Song 2", "Artist 2", 180))  
    player.addMusic(Music("Song 3", "Artist 3", 240))  
  

    player.play()  
    player.play()  
    player.play()  
  
  
    player.setPlayMode(PlayMode.Shuffle)  
    player.play()  
  

    player.deleteMusic(Music("Song 2", "Artist 2", 180))  
  
  
    player.play()  
  
  
    println(s"Current music list: ${player.getMusicList.mkString(", ")}")  
 
    player.reset()  
  }  
}
